/**
 *  Copyright Matt Silbernagel 2010
 *  
 *	This file is part of MobilePrinting.
 *
 *	MobilePrinting is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  MobilePrinting is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with MobilePrinting.  If not, see <http://www.gnu.org/licenses/>.
 */

package edu.uc.labs.MobilePrinting;

import static edu.uc.labs.MobilePrinting.Configurator.*;

import java.io.IOException;
import org.apache.log4j.Logger;

import edu.uc.labs.MobilePrinting.GUI.SysTray;
import edu.uc.labs.MobilePrinting.OSUtils.Installer;
import edu.uc.labs.MobilePrinting.OSUtils.NetworkManager;
import edu.uc.labs.MobilePrinting.OSUtils.StartupManager;
import edu.uc.labs.MobilePrinting.Prefs.Preferences;

/**
 * 
 * @author Matt Silbernagel
 */
public class MobilePrintRunner {


	private static NetworkManager net = null;
	private static Logger logger = Configurator.LOGGER;
	public static Preferences Prefs;
	public static SysTray prn;

	/**
	 * The class that implements the main service
	 * 
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String[] args) throws IOException {
		
		LOGGER.debug("Java library path set to: " + System.getProperty("java.library.path"));
		
		
		
		// Make sure all directories and
		// files that we need are in place
		Installer i = new Installer();
		if(i.install()){
			setupPrefs();
		} else {
			LOGGER.error("Can't start mobile printing until setup has finished");
			System.exit(1);
		}
		
		// Start a new thread for IPAddressFinder
		startIPAddressThread();
		

	}

	public static void setupPrefs(){
		Prefs = Configurator.getPrefs();
		logger.debug(Prefs.toString());
		// Set variables based on the returned Preference object
		Configurator.setLogLevel(Prefs.getLogLevel());
		Configurator.HTTP_SERVER = Prefs.getConfigServer();
		Configurator.PRINTERNAME = Prefs.getPrinterName();
		StartupManager sm = new StartupManager();
		sm.execute(Prefs);
		
	}
	
	public static void startIPAddressThread(){
		net = new NetworkManager();
		net.start();
	}
	
}
